//
//  YXWelcomeViewController.swift
//  微博YX
//
//  Created by apple on 15/11/26.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit

class YXWelcomeViewController: UIViewController {
    /// iconView底部约束
    private var iconViewBotttomCon: NSLayoutConstraint?
    
    
//    开始动画
    func moveAnimateion(){
        iconViewBotttomCon?.constant = -450
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
//           动画完成 通知切换界面
            print("动画完成")
                AppDelegate.switchViewController(true)
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimateion()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        
        if let avatar_large = YXUserAccount.loadUserAccount()?.avatar_large{
            iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        YXUserAccount.loadUserAccount()?.loadUserInfo()
        

       
    }
    /// MARK: - 添加约束
    // 准备UI
   private func prepareUI(){
//        添加子控件
    view.addSubview(bgView)
    view.addSubview(iconView)
    view.addSubview(messageLabel)
//    添加约束
    bgView.translatesAutoresizingMaskIntoConstraints = false
    iconView.translatesAutoresizingMaskIntoConstraints = false
    messageLabel.translatesAutoresizingMaskIntoConstraints = false
//    背景约束
   view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[biv]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bgView]))
   view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[biv]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bgView]))
// 头像约束
    view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier:1, constant:0))
    
    iconViewBotttomCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
    view.addConstraint(iconViewBotttomCon!)
//    宽度
    view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//    高度
    view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//    欢迎lable
    view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
    
    
    
    
 }
    
    
    
    /// MARK: - 懒加载控件
//    背景
    private lazy var bgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
//    头像
    private lazy var iconView: UIImageView = {
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 42.5
        icon.layer.masksToBounds = true
        return icon
    }()
//    消息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.sizeToFit()
        return label
    }()
    
}
